<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      ul {
        list-style: none;
      }

      .mi {
        position: relative;
        width: 223px;
        margin: 100px auto;
      }

      .mi .search-text {
        width: 223px;
        height: 48px;
        padding: 0 10px;
        font-size: 14px;
        line-height: 48px;
        border: 1px solid #e0e0e0;
        outline: none;
      }

      /* 搜索框边框颜色 */
      .mi .search {
        border: 1px solid #ff6700;
      }

      /* 下拉菜单 */
      .result-list {
        /* 默认隐藏 */
        display: none;
        position: absolute;
        left: 0;
        top: 48px;
        width: 223px;
        border: 1px solid #ff6700;
        border-top: 0;
        background: #fff;
      }

      .result-list a {
        display: block;
        padding: 6px 15px;
        font-size: 12px;
        color: #424242;
        text-decoration: none;
      }

      .result-list a:hover {
        background-color: #eee;
      }
    </style>
  </head>
  <body>
    <div class="mi">
      <input type="search" placeholder="小米笔记本" class="search-text" />
      <ul class="result-list">
        <li><a href="#">全部商品</a></li>
        <li><a href="#">小米11</a></li>
        <li><a href="#">小米10S</a></li>
        <li><a href="#">小米笔记本</a></li>
        <li><a href="#">小米手机</a></li>
        <li><a href="#">黑鲨4</a></li>
        <li><a href="#">空调</a></li>
      </ul>
    </div>
  </body>
</html>
<script>
  // 小米搜索框显示隐藏案例
  // 1. 先隐藏我们的下拉菜单
  // .result-list  display: none;
  // 2.获取焦点显示下拉框
  const search = document.querySelector(".search-text"); //获取搜索框焦点
  const ul = document.querySelector(".result-list"); //获取ul下拉焦点

  search.addEventListener("focus", function () {
    ul.style.display = "block"; //显示ul
    // search.style.border='1px solid red'
    search.classList.add("search"); //添加有边框的类属性--激活
  });
  // 3.失去焦点的时候隐藏下拉框，并且搜索框边框回复原来的灰色状态
  search.addEventListener("blur", function () {
    ul.style.display = "none"; //隐藏ul
    search.classList.remove("search"); //去掉类属性
    console.log("11");
  });
</script>
